import java.lang.reflect.Field;

public class AccessPersonFields {
    public static void main(String[] args) {
        try {
            //通过反射加载一个Person实例
            Class clz = Class.forName("pojo.Person");
            Object person = clz.newInstance();
            //获取private String name 属性
            Field name= clz.getDeclaredField("name");
            //通过setAccessible方法,设定可以访问
            name.setAccessible(true);
            //先取值看一下
            System.out.println("赋值前的name:"+name.get(person));
            //为name属性赋值
            name.set(person,"New pojo.Person");
            //展示一下赋值效果
            System.out.println("赋值后的name:"+name.get(person));
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
